package com.jintian.smart.kernel.redis.config.strategy;

import com.jintian.smart.kernel.common.util.StringUtil;
import com.jintian.smart.kernel.redis.config.RedissonProperties;
import com.jintian.smart.kernel.redis.constant.GlobalConstant;
import lombok.extern.slf4j.Slf4j;
import org.redisson.config.Config;

/**
 * 集群方式Redisson配置
 */
@Slf4j
public class ClusterRedissonConfigStrategyImpl implements RedissonConfigStrategy {
    @Override
    public Config createRedissonConfig(RedissonProperties redissonProperties) {
        Config config = new Config();
        try {
            String address = redissonProperties.getAddress();
            String password = redissonProperties.getPassword();
            String[] addrTokens = address.split(",");
            // 设置cluster节点的服务IP和端口
            for (int i = 0; i < addrTokens.length; i++) {
                config.useClusterServers().addNodeAddress(GlobalConstant.REDIS_CONNECTION_PREFIX.getConstant_value() + addrTokens[i]);
                if (StringUtil.isNotBlank(password)) {
                    config.useClusterServers().setPassword(password);
                }
            }
            config.useClusterServers().setPingConnectionInterval(redissonProperties.getPingInterval());
            config.useClusterServers().setSubscriptionConnectionMinimumIdleSize(redissonProperties.getMinIdleSize());
            config.useClusterServers().setMasterConnectionMinimumIdleSize(redissonProperties.getMinIdleSize());
            config.useClusterServers().setSlaveConnectionMinimumIdleSize(redissonProperties.getMinIdleSize());
            config.useClusterServers().setMasterConnectionPoolSize(redissonProperties.getPoolSize());
            config.useClusterServers().setSlaveConnectionPoolSize(redissonProperties.getPoolSize());
            config.setNettyThreads(redissonProperties.getNettyThreads());
            log.info("初始化[cluster]方式Config,redisAddress:" + address);
        } catch (Exception e) {
            log.error("cluster Redisson init error", e);
            e.printStackTrace();
        }
        return config;
    }
}
